#==========================================================================
#
# Traffic gererator types of files is IPACC3.
#
# Special for testing correct work of the traffic processor.
#
#==========================================================================
import sys
import os
import random

#if len( sys.argv ) == 1:
#    print "Usage: .py run"
#    exit

def ips_load( fn ):
    return map( lambda s: s[:len(s) - 1], file( fn ).xreadlines() )

#---------------------------------------------------------#
# The configuration part.
#---------------------------------------------------------#

ip_classes = [
   ips_load( "ip_0.txt" ),
   ips_load( "ip_1.txt" ),
]

traffic_file     = "./20090319160000.ipacc";
traffic_classify = "./etalon.traffic";

#---------------------------------------------------------#
# The script part.
#---------------------------------------------------------#
cl = {}

big_index = 0
ftrf = file( traffic_file, "wb" )
ftrf.write( """20090319160000

   Source           Destination    Packets        Bytes  SrcPt DstPt Proto   IF
""")

fcls = file( traffic_classify, "wb" )

while big_index < 100000:
    big_index += 1

    class_0 = 0
    class_1 = random.randint( 0, len( ip_classes ) - 1 )

    index_0 = random.randint( 0, len( ip_classes[ class_0 ] ) - 1 )
    index_1 = random.randint( 0, len( ip_classes[ class_1 ] ) - 1 )

    if index_0 == index_1:
        index_1 = ( index_1 + 1 ) % ( len( ip_classes[ class_1 ] ) - 1 )

    ip_src = ip_classes[ class_0 ][ index_0 ]
    ip_dst = ip_classes[ class_1 ][ index_1 ]
    bytes  = random.randint( 64, 32000 )

    class_trf = str(int(not class_0 == class_1))

    if cl.has_key( ip_src + class_trf ):
        cl[ ip_src + class_trf ] = [ int(class_trf),
                                     cl[ ip_src + class_trf ][ 1 ],
                                     cl[ ip_src + class_trf ][ 2 ] + bytes ]
    else:
        cl[ ip_src + class_trf ] = [ int(class_trf), 0, bytes ]

    if cl.has_key( ip_dst + class_trf ):
        cl[ ip_dst + class_trf ] = [ int(class_trf),
                                     cl[ ip_dst + class_trf ][ 1 ] + bytes,
                                     cl[ ip_dst + class_trf ][ 2 ] ]
    else:
        cl[ ip_dst + class_trf ] = [ int(class_trf), bytes, 0 ]


    ftrf.write( " %-20s %-19s     1    %-10d     80   80    6    ipq\n" % ( ip_src, ip_dst, bytes ) )

for ip in cl:
     val = cl[ ip ]
     fcls.write( "%s;%ld;%ld;%ld\n" % ( ip[ :len(ip) - 1 ], val[ 0 ], val[ 1 ], val[ 2 ] ) )

ftrf.close()
fcls.close()
